package com.example.sqlite;

import static androidx.constraintlayout.helper.widget.MotionEffect.TAG;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.nfc.Tag;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_tele;
    private TextView tv_name;
    private Button btn_call;
    String sTele = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_tele = (EditText) findViewById(R.id.et_tele);
        tv_name = (TextView) findViewById(R.id.tv_name);
        btn_call = (Button) findViewById(R.id.btn_call);
        //申请通讯录读取权限判断
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.READ_CONTACTS)
                == PackageManager.PERMISSION_GRANTED){//应用需求权限是否已经授权0已授权-1没有授权

        }  else {
            ActivityCompat.requestPermissions(this ,new String[]{android.Manifest.permission.READ_CONTACTS},1);

        }
        //申请拨号权限判断
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.CALL_PHONE)
                == PackageManager.PERMISSION_GRANTED){//应用需求权限是否已经授权0已授权-1没有授权

        }  else {
            ActivityCompat.requestPermissions(this ,new String[]{android.Manifest.permission.CALL_PHONE},1);

        }


        btn_call.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {

        sTele = et_tele.getText().toString();
        if (!"".equals(sTele)) {
            //判断号码的主人
            testReadNameByPhone(sTele);
            //启动拨号程序
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_CALL);
            // url:统一资源定位符
            intent.setData(Uri.parse("tel:" + sTele));
            // 开启系统拨号器
            startActivity(intent);
        }
    }

    @SuppressLint("Range")
    public void testReadNameByPhone(String sTele) {
        String phone = sTele;
        Uri uri = Uri.parse("content://com.android.contacts/data/phones/filter/" + phone);
        ContentResolver resolver = getContentResolver();
        // 从raw_contact表中查询display_name
        Cursor cursor = resolver.query(uri, new String[]{android.provider.ContactsContract.Contacts.DISPLAY_NAME}, null, null, null);
        if (cursor.moveToFirst()) {
            tv_name.setText(cursor.getString(0));   //  显示name
            // Log.d(TAG,cursor.getString(cursor.getColumnIndex(android.provider.ContactsContract.Contacts.DISPLAY_NAME)));
            // Toast.makeText(this, cursor.getString(cursor.getColumnIndex(android.provider.ContactsContract.Contacts.DISPLAY_NAME)), Toast.LENGTH_SHORT).show();
        } else {
            tv_name.setText("陌生人");
            //Log.d(TAG,cursor.getString(cursor.getColumnIndex(android.provider.ContactsContract.Contacts.DISPLAY_NAME)));
            //Toast.makeText(this, cursor.getString(cursor.getColumnIndex(android.provider.ContactsContract.Contacts.DISPLAY_NAME)), Toast.LENGTH_SHORT).show();

        }

    }
}